<template>
  <div class="load-adjustment-flow">
    <h4>负载调节流程</h4>
    <div class="main-container">
      <!-- 左侧流程步骤区域 -->
      <div class="flow-container">
        <!-- 步骤一 -->
        <div class="flow-step step-one" @click="showStepOneDialog = true">
          <div class="step-number">1</div>
          <div class="step-content">
            <h5>检查机组控制屏</h5>
            <p>查看负载接入情况</p>
          </div>
        </div>

        <!-- 流程箭头 -->
        <div class="flow-arrow">
          <svg width="40" height="20" viewBox="0 0 40 20">
            <line
              x1="0"
              y1="10"
              x2="30"
              y2="10"
              stroke="#3498db"
              stroke-width="2"
            />
            <polygon points="30,5 40,10 30,15" fill="#3498db" />
          </svg>
        </div>

        <!-- 步骤二 -->
        <div
          class="flow-step step-two"
          :class="{
            completed: loadAdjustmentStore.getStepTwoStatus,
            unlocked: loadAdjustmentStore.getStepOneStatus,
          }"
        >
          <div class="step-number">2</div>
          <div class="step-content">
            <h5>调节负载</h5>
            <p>根据实际情况调整负载</p>
          </div>
          <button
            class="adjust-load-button"
            :disabled="!loadAdjustmentStore.getStepOneStatus"
            @click="showAdjustLoadDialog = true"
          >
            调节负载
          </button>
        </div>

        <!-- 流程箭头 -->
        <div class="flow-arrow">
          <svg width="40" height="20" viewBox="0 0 40 20">
            <line
              x1="0"
              y1="10"
              x2="30"
              y2="10"
              stroke="#3498db"
              stroke-width="2"
            />
            <polygon points="30,5 40,10 30,15" fill="#3498db" />
          </svg>
        </div>

        <!-- 步骤三 -->
        <div class="flow-step step-three" :class="{ completed: false }">
          <div class="step-number">3</div>
          <div class="step-content">
            <h5>检查趋势曲线</h5>
            <p>查看4组发电机定子绕组温度变量</p>
          </div>
          <button
            class="view-trend-button"
            :disabled="!loadAdjustmentStore.getStepTwoStatus"
            @click="openTrendChart"
          >
            查看趋势曲线
          </button>
        </div>
      </div>
    </div>

    <!-- 步骤一的提示对话框 -->
    <div
      v-if="showStepOneDialog"
      class="dialog-overlay"
      @click="closeStepOneDialog"
    >
      <div class="dialog-content" @click.stop>
        <h5>检查机组控制屏的发电机面板</h5>
        <p>请在发电机面板上查看负载接入情况，确认当前的负载状态。</p>
        <div class="dialog-buttons">
          <button class="cancel-button" @click="closeStepOneDialog">
            取消
          </button>
          <button class="confirm-button" @click="openGeneratorPanel">
            打开发电机面板
          </button>
        </div>
      </div>
    </div>

    <!-- 步骤二的负载调节对话框 -->
    <div
      v-if="showAdjustLoadDialog"
      class="dialog-overlay"
      @click="closeAdjustLoadDialog"
    >
      <div class="dialog-content" @click.stop>
        <h5>负载调节</h5>
        <p class="status-info" v-if="isOverloadActive">
          当前检测到运行过载状态
        </p>
        <p>点击下方按钮将负载参数重置为正常值。</p>
        <div class="dialog-buttons">
          <button class="cancel-button" @click="closeAdjustLoadDialog">
            取消
          </button>
          <button
            class="confirm-button adjust-button"
            :disabled="loadAdjustmentStore.getAdjustingStatus"
            @click="adjustLoad"
          >
            {{
              loadAdjustmentStore.getAdjustingStatus ? "调节中..." : "重置负载"
            }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { ElMessage } from "element-plus";
import { setAlgorithmParam } from "@/api/util";
import { useUserStore } from "@/store/user";
import { useLoadAdjustmentStore } from "@/store/loadAdjustment";

// 定义组件props
const props = defineProps({
  isOverloadActive: {
    type: Boolean,
    default: false,
  },
});

// 获取用户store实例
const userStore = useUserStore();
// 获取负载调节store实例
const loadAdjustmentStore = useLoadAdjustmentStore();

// 定义组件事件
const emit = defineEmits([
  "open-generator-panel",
  "load-adjusted",
  "open-trend-chart",
]);

// 对话框状态管理（组件内部状态）
const showStepOneDialog = ref(false);
const showAdjustLoadDialog = ref(false);

// 关闭步骤一对话框
const closeStepOneDialog = () => {
  showStepOneDialog.value = false;
};

// 关闭步骤二对话框
const closeAdjustLoadDialog = () => {
  showAdjustLoadDialog.value = false;
};

// 打开发电机面板
const openGeneratorPanel = () => {
  // 使用store的action设置步骤一为已完成
  loadAdjustmentStore.completeStepOne();
  showStepOneDialog.value = false;
  // 触发事件通知父组件打开发电机面板
  emit("open-generator-panel");
};

// 调节负载函数
const adjustLoad = async () => {
  try {
    // 使用store设置调节状态
    loadAdjustmentStore.setAdjustingStatus(true);
    // 从user store获取copyNum
    const copyNum = userStore.copyNum;
    // 调用setAlgorithmParam设置"2_18_0_0"值为0，传入copyNum参数
    await setAlgorithmParam("2_18_0_0", 0, copyNum);

    // 显示成功提示
    ElMessage.success("负载调节成功");

    // 关闭对话框
    showAdjustLoadDialog.value = false;

    // 使用store标记步骤二为已完成
    loadAdjustmentStore.completeStepTwo();

    // 触发负载已调节事件
    emit("load-adjusted");
  } catch (error) {
    console.error("调节负载失败:", error);
    ElMessage.error("负载调节失败，请稍后重试");
  } finally {
    // 使用store重置调节状态
    loadAdjustmentStore.setAdjustingStatus(false);
  }
};

// 打开趋势曲线图表
const openTrendChart = () => {
  // 关闭所有对话框
  showStepOneDialog.value = false;
  showAdjustLoadDialog.value = false;

  // 触发事件通知父组件打开趋势图表
  emit("open-trend-chart");
};
</script>

<style scoped>
.load-adjustment-flow {
  background: #2c3e50;
  border-radius: 8px;
  padding: 20px;
  color: #ecf0f1;
}

.main-container {
  display: flex;
  gap: 20px;
  align-items: flex-start;
}

.load-adjustment-flow h4 {
  color: #3498db;
  margin-top: 0;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #34495e;
  font-size: 18px;
  font-weight: 600;
}

.flow-container {
  display: flex;
  flex-direction: column;
  gap: 20px;
  flex: 1;
}

.flow-step {
  background: #34495e;
  border-radius: 8px;
  padding: 16px;
  transition: all 0.3s ease;
}

.step-one {
  cursor: pointer;
  border-left: 4px solid #3498db;
}

.step-one:hover {
  background: #3c566a;
  transform: translateX(5px);
}

.step-two {
  border-left: 4px solid #7f8c8d;
}

.step-two.unlocked {
  border-left: 4px solid #3498db;
  cursor: pointer;
  transition: all 0.3s ease;
}

.step-two.unlocked:hover {
  background: #3c566a;
  transform: translateX(5px);
}

.step-two.completed {
  border-left: 4px solid #27ae60;
}

.step-three {
  border-left: 4px solid #7f8c8d;
}

.step-three.unlocked {
  border-left: 4px solid #3498db;
  cursor: pointer;
  transition: all 0.3s ease;
}

.step-three.unlocked:hover {
  background: #3c566a;
  transform: translateX(5px);
}

.step-three.completed {
  border-left: 4px solid #2ecc71;
}

.flow-step h5 {
  margin: 0 0 8px 0;
  color: #3498db;
  font-size: 16px;
  font-weight: 500;
}

.flow-step p {
  margin: 0 0 12px 0;
  color: #bdc3c7;
  font-size: 14px;
}

.step-number {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #3498db;
  color: white;
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 8px;
}

.adjust-load-button,
.view-trend-button {
  background: #3498db;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s ease;
}

.adjust-load-button:hover:not(:disabled),
.view-trend-button:hover:not(:disabled) {
  background: #2980b9;
}

.adjust-load-button:disabled,
.view-trend-button:disabled {
  background: #7f8c8d;
  cursor: not-allowed;
}

.flow-arrow {
  margin: 0 auto;
  width: 40px;
  height: 20px;
}

/* 对话框样式 */
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.dialog-content {
  background: #2c3e50;
  border-radius: 8px;
  padding: 24px;
  width: 90%;
  max-width: 400px;
  color: #ecf0f1;
}

.dialog-content h5 {
  margin-top: 0;
  color: #3498db;
  font-size: 18px;
}

.dialog-content p {
  color: #bdc3c7;
  margin-bottom: 20px;
}

.dialog-content .status-info {
  color: #e74c3c;
  font-weight: 500;
}

.dialog-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.cancel-button {
  background: #7f8c8d;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.cancel-button:hover {
  background: #95a5a6;
}

.confirm-button {
  background: #3498db;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.confirm-button:hover:not(:disabled) {
  background: #2980b9;
}

.confirm-button:disabled {
  background: #7f8c8d;
  cursor: not-allowed;
}


</style>